#coding=utf8
import random
import xlrd
from django.http import HttpResponse
from datetime import date

class Folder:
  def __init__(self, name):
    self.name = name
    self.list = ''
  def __cmp__(self, name):
    if isinstance(folder, Folder):
      return cmp(self.name, folder.name)
    else:
      return cmp(self.name, folder)
  def __str__(self):
    return self.name
  def append(self, point):
    self.list += point

def kml(request):
  kml = (
    '<?xml version="1.0" encoding="UTF-8"?>\n'
    '<kml xmlns="http://www.opengis.net/kml/2.2">\n'
    '<Document>\n'
    '<open>1</open>'
    u'<name>勘察设计进度通报</name>\n'
    )
  xls = xlrd.open_workbook(u"./static/upload/勘察设计进度通报.xls")
  sheet = xls.sheet_by_index(3)
  n = sheet.nrows
  folders = []
  dates = []
  delta = date(1899, 12, 31).toordinal() - 1
  i=2
  while i<n:
    if sheet.cell_value(rowx=i,colx=10).find('ok')==-1:
      i += 1
      continue
    if (sheet.cell_value(rowx=i,colx=3)!=''):
      name = sheet.cell_value(rowx=i,colx=3)
    name = name + '_' + sheet.cell_value(rowx=i,colx=0)
    latitude = sheet.cell_value(rowx=i,colx=4)
    longitude = sheet.cell_value(rowx=i,colx=5)
    jianshefangshi = ''
    jifang = ''
    tianxian = ''
    guagao = ''
    result = ''
    if (sheet.cell_value(rowx=i,colx=6)!=''):
      jianshefangshi = sheet.cell_value(rowx=i,colx=6)
    if (sheet.cell_value(rowx=i,colx=7)!=''):
      jifang = sheet.cell_value(rowx=i,colx=7)
    if (sheet.cell_value(rowx=i,colx=8)!=''):
      tianxian = sheet.cell_value(rowx=i,colx=8)
    if (sheet.cell_value(rowx=i,colx=9)!=''):
      guagao = int(sheet.cell_value(rowx=i,colx=9))
    if (sheet.cell_value(rowx=i,colx=11)!=''):
      result = sheet.cell_value(rowx=i,colx=11)
    point = (
      '<Placemark>\n'
      '<name>%s</name>\n'
      '<Point>\n'
      '<coordinates>%.6f,%.6f</coordinates>\n'
      '</Point>\n'
      '<description>'
      '<table width="400" border="1">'
      '<tr>'
      u'<td colspan="2" align="center">纬度，经度</td>'
      '<td colspan="2" align="center">%.6f,%.6f</td>'
      '</tr>'
      '<tr>'
      u'<td width="80">建设方式</td>'
      '<td width="120">%s</td>'
      u'<td width="80">机房</td>'
      '<td width="120">%s</td>'
      '</tr>'
      '<tr>'
      u'<td>天线</td>'
      '<td>%s</td>'
      u'<td>挂高</td>'
      u'<td>%s米</td>'
      '</tr>'
      '<tr>'
      u'<td>勘察评审结论</td>'
      '<td colspan="3">%s</td>'
      '</tr>'
      '</table>'
      '</description>\n'
      '</Placemark>\n'
      ) %(name, longitude, latitude, latitude, longitude, jianshefangshi, jifang, tianxian, guagao, result)
    d = date.fromordinal(delta + int(sheet.cell_value(rowx=i,colx=12))).strftime("%Y%m%d")
    if(dates.count(d)==0):
      dates.append(d)
      f = Folder(d)
      f.append(point)
      folders.append(f)
    else:
      for f in folders:
        if (f.name == d):
          f.append(point)
    i += 1
  #输出逻辑
  count_f = 0
  folders_len = len(folders)
  for f in folders:
    count_f += 1
    if count_f < folders_len:
      kml= kml + '<Folder>\n<name>' + f.name + '</name>\n' + f.list + '</Folder>\n'
    else:
      kml= kml + '<Folder><open>1</open>\n<name>' + f.name + u'(本次通报)</name>\n' + f.list + '</Folder>\n'
  kml += '</Document>\n</kml>\n'
  #以上输出逻辑
  response = HttpResponse()
  response['Content-Disposition'] = 'attachment;filename=勘察设计进度通报.kml'
  response['Content-Type'] = 'application/vnd.google-earth.kml+xml'
  response.write(kml)
  return response
